package com.kexuexiong.demo.bear.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kexuexiong.demo.bear.common.multiDataSource.annotation.SwitchDataSource;
import com.kexuexiong.demo.bear.common.utils.SpringContextUtil;
import com.kexuexiong.demo.bear.entity.Person;
import com.kexuexiong.demo.bear.mapper.PersonMapper;
import com.kexuexiong.demo.bear.service.PersonService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kexuexiong
 * @since 2023-03-22
 */
@Service
@Slf4j
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @Override
    @SwitchDataSource("demo-02")
    public void saveV1(Person params) {
        this.save(params);
    }

    @Override
    @GlobalTransactional(name = "saveV2",rollbackFor = Exception.class)
    public void saveV2(Person params) {
        log.info("seata xid :{}", RootContext.getXID());

        PersonServiceImpl personService = SpringContextUtil.getBean(PersonServiceImpl.class);
        personService.saveDemo(params);
        params.setId(null);
        personService.saveDemo02(params);
    }

    @Override
    @SwitchDataSource
    public void saveDemo(Person person) {
        this.save(person);
    }

    @Override
    @SwitchDataSource("demo-02")
    public void saveDemo02(Person person) {
        this.save(person);
    }


}
